FROM {{ namespace }}/{{ image_prefix }}openstack-base:{{ tag }}
{% block labels %}
LABEL maintainer="{{ maintainer }}" name="{{ image_name }}" build-date="{{ build_date }}"
{% endblock %}

{% block gnocchi_base_header %}{% endblock %}

{% import "macros.j2" as macros with context %}

{{ macros.configure_user(name='gnocchi') }}

{{ macros.enable_extra_repos(['ceph']) }}

{% if install_type == 'binary' %}

{# NOTE(mgoddard): EPEL required for python3-boto3 package on CentOS 8 #}
{{ macros.enable_extra_repos(['epel']) }}

    {% if base_package_type == 'rpm' %}

        {% set gnocchi_base_packages = [
            'gnocchi-common',
            'httpd',
            'mod_ssl',
            'python3-boto3',
            'python3-cradox',
            'python3-ldappool',
            'python3-mod_wsgi'
        ] %}
{{ macros.install_packages(gnocchi_base_packages | customizable("packages")) }}
RUN sed -i -r 's,^(Listen 80),#\1,' /etc/httpd/conf/httpd.conf \
    && sed -i -r 's,^(Listen 443),#\1,' /etc/httpd/conf.d/ssl.conf
    {% elif base_package_type == 'deb' %}

       {% set gnocchi_base_packages = [
            'apache2',
            'build-essential',
            'gnocchi-common',
            'libapache2-mod-wsgi-py3',
            'librados-dev',
            'python3-boto3',
            'python3-dev',
            'python3-ldappool',
            'python3-pip',
            'python3-rados',
            'python3-setuptools',
            'python3-wheel'
        ] %}
        {% set gnocchi_base_pip_packages = [
            'cradox'
        ] %}

{{ macros.install_packages(gnocchi_base_packages | customizable("packages")) }}
RUN {{ macros.install_pip(gnocchi_base_pip_packages | customizable("pip_packages"), constraints=false, pip_version="pip3") }} \
    && truncate -s 0 /etc/apache2/ports.conf

    {% endif %}
{% elif install_type == 'source' %}
    {% if base_package_type == 'rpm' %}

       {% set gnocchi_base_packages = [
            'httpd',
            'librados2-devel',
            'mod_ssl',
            'python3-ldappool',
            'python3-mod_wsgi'
        ] %}

        # on x86-64 pip grabs precompiled numpy/scipy - on aarch64/ppc64le
        # it needs to be built

        {% if base_arch != 'x86_64' %}
            {% set gnocchi_base_packages = gnocchi_base_packages + [
                'blas-devel',
                'lapack-devel'
            ] %}
        {% endif %}

{{ macros.install_packages(gnocchi_base_packages | customizable("packages")) }}
RUN mkdir -p /var/www/cgi-bin/gnocchi \
    && sed -i -r 's,^(Listen 80),#\1,' /etc/httpd/conf/httpd.conf \
    && sed -i -r 's,^(Listen 443),#\1,' /etc/httpd/conf.d/ssl.conf
    {% elif base_package_type == 'deb' %}

       {% set gnocchi_base_packages = [
            'apache2',
            'libapache2-mod-wsgi-py3',
            'librados-dev',
            'python3-ldappool'
        ] %}

        # on x86-64 pip grabs precompiled numpy/scipy - on aarch64/ppc64le
        # it needs to be built

        {% if base_arch != 'x86_64' %}
            {% set gnocchi_base_packages = gnocchi_base_packages + [
                'libblas-dev',
                'liblapack-dev'
            ] %}
        {% endif %}

{{ macros.install_packages(gnocchi_base_packages | customizable("packages")) }}
RUN truncate -s 0 /etc/apache2/ports.conf

   {% endif %}
ADD gnocchi-base-archive /gnocchi-base-source

{% set gnocchi_base_pip_packages = [
    'gnocchiclient',
    '/gnocchi[keystone,mysql,file,ceph,s3]'
] %}

RUN ln -s gnocchi-base-source/* gnocchi \
    && {{ macros.install_pip(gnocchi_base_pip_packages | customizable("pip_packages")) }} \
    && mkdir -p /etc/gnocchi \
    && chown -R gnocchi: /etc/gnocchi

{% endif %}

COPY extend_start.sh /usr/local/bin/kolla_extend_start
COPY gnocchi_sudoers /etc/sudoers.d/kolla_gnocchi_sudoers

RUN chmod 750 /etc/sudoers.d \
    && chmod 640 /etc/sudoers.d/kolla_gnocchi_sudoers \
    && touch /usr/local/bin/kolla_gnocchi_extend_start \
    && chmod 755 /usr/local/bin/kolla_extend_start /usr/local/bin/kolla_gnocchi_extend_start

{% block gnocchi_base_footer %}{% endblock %}
